from django.shortcuts import render, redirect
from app_blog.models import Blog, Advertisement
from app_article.models import Comment, Category, Tags, Article
from django.views import View
from lib.CommonResponse import CommonResponse
from lib.CommonPager import Pagination
from django.contrib.auth.decorators import login_required


def pager(request, article_all):
    # 制作分页器
    _current_page = request.GET.get('page', 1)
    _all_count = article_all.count()
    _page_obj = Pagination(current_page=_current_page, all_count=_all_count, per_page_num=10, pager_count=5)
    if not article_all:
        article_data = article_all
    else:
        article_data = article_all[_page_obj.start:_page_obj.end]
    page_html = _page_obj.page_html()
    return article_data, page_html


def data_all(request):
    blog_obj = Blog.objects.get(userinfo=request.user)
    # 统计每个分类
    category_all = Category.objects.filter(blog=blog_obj)
    # 统计每个标签
    tags_all = Tags.objects.filter(blog=blog_obj)
    # 统计当前用户的所以评论
    comment_all = Comment.objects.filter(user=request.user).order_by('-create_time')

    return blog_obj, category_all, tags_all, comment_all


@login_required
def backend(request):
    blog_obj, category_all, tags_all, comment_all = data_all(request)
    article_all = Article.objects.filter(blog=blog_obj).exclude(is_delete=True).order_by('-create_time')
    article_all, page_html = pager(request, article_all)
    return render(request, 'backend.html', locals())


@login_required
def delete(request, condition, pk):
    if condition == 'category':
        Category.objects.filter(pk=pk).delete()
    elif condition == 'tag':
        Tags.objects.filter(pk=pk).delete()
    elif condition == 'comment':
        Comment.objects.filter(pk=pk).delete()
    elif condition == 'advertisement':
        Advertisement.objects.filter(pk=pk).delete()
    return redirect('backend:manage', condition=condition)


class AddAdv(View):
    def get(self, request, *args, **kwargs):
        blog_obj = Blog.objects.get(userinfo=request.user)
        return render(request, 'add_adv.html', locals())

    def post(self, request, *args, **kwargs):
        data = request.POST
        file_obj = request.FILES.get('imgFile')
        title = data.get('title')
        content = data.get('content')
        phone = data.get('phone')
        is_background = data.get('is_background')
        is_background = True if is_background == 'on' else False
        if not file_obj:
            Advertisement.objects.create(title=title, content=content, phone=phone, is_background=is_background)
        else:
            Advertisement.objects.create(title=title, content=content, phone=phone, is_background=is_background,
                                         img=file_obj)
        return CommonResponse(code=200, msg=f'添加广告 {title} 成功')


def manageCategory(request, condition):
    blog_obj, category_all, tags_all, comment_all = data_all(request)
    article_all = Article.objects.filter(blog=blog_obj).exclude(is_delete=True).order_by('-create_time')
    if condition == 'category':
        data_obj = Category.objects.filter(blog=blog_obj)
    elif condition == 'tag':
        data_obj = Tags.objects.filter(blog=blog_obj)
    elif condition == 'advertisement':
        data_obj = Advertisement.objects.all()
    return render(request, 'manage_backend.html', locals())


def recycleBin(request):
    blog_obj = Blog.objects.get(userinfo=request.user)
    # 过滤出字段is_delete为true的文章
    article_all = Article.objects.filter(blog__userinfo=request.user).filter(is_delete=True)
    return render(request, 'recycle_bin.html', locals())


def revoke(request, pk):
    # 撤销删除
    Article.objects.filter(pk=pk).update(is_delete=False)
    return redirect('backend:recycle')


class EditCategoryTag(View):
    def get(self, request):
        data = request.GET
        condition = data.get('condition')
        pk = data.get('pk')
        if condition == 'category':
            obj = Category.objects.get(pk=pk)
        elif condition == 'tag':
            obj = Tags.objects.get(pk=pk)
        name = obj.name
        blog = obj.blog.name
        return CommonResponse(name=name, blog=blog)

    def post(self, request):
        if request.is_ajax():
            data = request.POST
            old_name = data.get('oldName')
            name = data.get('category')
            blog = data.get('blog')
            condition = data.get('condition')
            if condition == 'category':
                Category.objects.filter(blog__name=blog).filter(name=old_name).update(name=name)
            elif condition == 'tag':
                Tags.objects.filter(blog__name=blog).filter(name=old_name).update(name=name)
            return CommonResponse(code=200)


class AddCategoryTag(View):
    def get(self, request, condition):
        blog_obj, category_all, tags_all, comment_all = data_all(request)
        return render(request, 'add_category.html', locals())

    def post(self, request, condition):
        if request.is_ajax():
            data = request.POST
            name = data.get(condition)
            blog_obj = Blog.objects.get(userinfo=request.user)
            if condition == 'category':
                obj = Category.objects
            elif condition == 'tag':
                obj = Tags.objects
            obj.create(name=name, blog=blog_obj)
            return CommonResponse(code=200, msg=f'{name} 创建成功')


class EditAdvertisement(View):
    def get(self, request):
        data = request.GET
        pk = data.get('pk')
        obj = Advertisement.objects.get(pk=pk)
        title = obj.title
        content = obj.content
        img = obj.img
        phone = obj.phone
        is_background = obj.is_background
        return CommonResponse(title=title, content=content, img=str(img),
                              phone=phone, is_background=is_background
                              )

    def post(self, request):
        if request.is_ajax():
            data = request.POST
            file_obj = request.FILES.get('new_adv_img')
            old_title = data.get('old_title')
            title = data.get('title')
            content = data.get('content')
            phone = data.get('phone')
            is_background = data.get('is_background')
            is_background = True if is_background == 'on' else False
            if not file_obj:
                Advertisement.objects.filter(title=old_title).update(title=title, phone=phone, content=content,
                                                                     is_background=is_background)
            else:
                Advertisement.objects.filter(title=old_title).update(title=title, phone=phone, content=content,
                                                                     is_background=is_background, img=file_obj)
            return CommonResponse(msg=f"广告{title}更新成功")
